package com.hunliji.monitor.web.facade.impl;

import com.alibaba.fastjson.JSON;
import com.hunliji.monitor.service.api.CustomerInfoService;
import com.hunliji.monitor.web.facade.CustomerMonitorFacade;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 客资
 * @author wuchen
 */
@Service
public class CustomerMonitorFacadeImpl implements CustomerMonitorFacade {
    private static final Logger logger = LoggerFactory.getLogger(CustomerMonitorFacadeImpl.class);
    @Resource
    private CustomerInfoService customerInfoService;
    @Override
    public void todayAndYesterdayCountStatics() {
        try{
            Long timeStamp = System.currentTimeMillis();
            Date currentDate = new Date(timeStamp);
            Date yesterdayCurrentDate = new Date(timeStamp - 24*60*60*1000);
            Date zeroDate = getDayOfZero(currentDate);
            Date yesterdayZeroDate = getDayOfZero(yesterdayCurrentDate);
            Integer todayCount = customerInfoService.countByDate(zeroDate,currentDate);
            Integer yesterdayCount = customerInfoService.countByDate(yesterdayZeroDate,yesterdayCurrentDate);
            Double rate = (todayCount-yesterdayCount*1.0)/yesterdayCount;

            CountStatistic countStatistic = new CountStatistic();
            countStatistic.setTodayCount(todayCount);
            countStatistic.setYesterdayCount(yesterdayCount);
            countStatistic.setName("客资数量统计");
            countStatistic.setRate(rate);
            logger.warn("{}", JSON.toJSONString(countStatistic));
        }catch (Exception e){
            logger.error("客资统计异常",e);
        }

    }
    @Data
    public static class CountStatistic{
        private String name;
        private Integer todayCount;
        private Integer yesterdayCount;
        private Double rate;
    }

    private Date getDayOfZero(Date date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date zeroDate = sdf.parse(sdf.format(date));
        return zeroDate;
    }
}
